/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.com.leon.ui.tree;


import cn.com.leon.utils.ListFiles;
import cn.com.leon.ui.MainFace;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author Justin
 */
class DirSelectionListener implements TreeSelectionListener {

        public void valueChanged(TreeSelectionEvent event) {
            DefaultMutableTreeNode node = getTreeNode(event.getPath());
            FileNode fnode = getFileNode(node);
            if (fnode != null) {
                MainFace.display.setText(fnode.getFile().getAbsolutePath());
                ListFiles.openfiles(fnode);
            } else {
                MainFace.display.setText("");
            }
        }
         DefaultMutableTreeNode getTreeNode(TreePath path) {
        return (DefaultMutableTreeNode) (path.getLastPathComponent());
    }

    FileNode getFileNode(DefaultMutableTreeNode node) {
        if (node == null) {
            return null;
        }
        Object obj = node.getUserObject();
        if (obj instanceof IconData) {
            obj = ((IconData) obj).getObject();
        }
        if (obj instanceof FileNode) {
            return (FileNode) obj;
        } else {
            return null;
        }
    }
}
